﻿namespace NTLib.Core.Shared
{
    using System;

    /// <summary>
    /// Implement a unique key by pairing the ISharedValue hashCode and the ISharedValueKey HashCode using the cantor hash.
    /// </summary>
    public class SharedValueKey : ISharedValueKey
    {
        #region Fields

        private readonly Int32 _uniqueKey;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="SharedValueKey" /> class.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="propName">Name of the property.</param>
        public SharedValueKey(ISharedValue value, string propName)
        {
            var hashValue = value.GetHashCode();
            var hash = base.GetHashCode();
            this._uniqueKey = hashValue ^ hash;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Equalses the specified other.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public bool Equals(ISharedValueKey other)
        {
            if (other == null)
                return false;

            if (object.ReferenceEquals(this, other))
                return true;

            return this._uniqueKey == other.GetHashCode();
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return this._uniqueKey;
        }

        #endregion
    }
}
